package br.edu.ifrn.persistencia;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Conexao {
	private final static String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
	private final static String url = "jdbc:sqlserver://localhost:1433;databaseName=Proex";
	private final static String usuario = "sa";
	private final static String senha = "senha";

	private Connection connection;
	private static Conexao singleton = new Conexao();

	public static Conexao getInstance() {
		return Conexao.singleton;
	}

	private Conexao() {
		super();
		this.init(driver, url, usuario, senha);
	}

	public void init(String driver, String url, String usuario, String senha) {
		try {
			Class.forName(driver);
			this.connection = DriverManager.getConnection(url, usuario, senha);
		} catch (ClassNotFoundException e) {
			System.out.println("Classe nao encontrada!");
			e.printStackTrace();
		} catch (SQLException e) {
			System.out.println("Error no SQL!");
			e.printStackTrace();
		}
	}

	public ResultSet executeQueryStatement(String comando) {
		try {
			Statement statement = connection.createStatement();
			return statement.executeQuery(comando);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}

	public int executeUpdateStatement(String comando) {
		try {
			Statement statement = connection.createStatement();
			return statement.executeUpdate(comando);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return -1;
	}

	public PreparedStatement getPreparedStatement(String comando) {
		try {
			return connection.prepareStatement(comando);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
